package com.sesame.sysuser.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sesame.sysuser.bean.SysUser;
import com.sesame.sysuser.dao.SysUserDao;
import kim.sesame.common.utils.ObjectUtil;
import lombok.extern.apachecommons.CommonsLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * SysUserService
 *
 * @author admin
 * @date 2019-02-16 10:22:58
 * @Description: 系统用户, 测试mybatis-plus
 */
@CommonsLog
@Service
public class SysUserService {

    @SuppressWarnings("all")
    @Autowired
    private SysUserDao sysUserDao;

    /**
     * 公用的查询条件,不要随便修改
     *
     * @param bean 字段
     * @return queryWrapper
     */
    public LambdaQueryWrapper<SysUser> getLQueryWrapper(SysUser bean) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        if (bean == null) {
            return queryWrapper;
        }
        queryWrapper
                .eq(ObjectUtil.isNotEmpty(bean.getId()), SysUser::getId, bean.getId())
                .eq(ObjectUtil.isNotEmpty(bean.getName()), SysUser::getName, bean.getName())
                .eq(ObjectUtil.isNotEmpty(bean.getAaAge()), SysUser::getAaAge, bean.getAaAge())
                .eq(ObjectUtil.isNotEmpty(bean.getAaBbbEmail()), SysUser::getAaBbbEmail, bean.getAaBbbEmail())
                .eq(ObjectUtil.isNotEmpty(bean.getActive()), SysUser::getActive, bean.getActive())
                .eq(ObjectUtil.isNotEmpty(bean.getCreateTime()), SysUser::getCreateTime, bean.getCreateTime())
                .eq(ObjectUtil.isNotEmpty(bean.getCreateUser()), SysUser::getCreateUser, bean.getCreateUser())
        ;
        return queryWrapper;
    }

    /**
     * 查询list
     *
     * @author admin
     * @date 2019-02-16 10:22:58
     * @Description: 分页时要注意
     */
    public List<SysUser> searchList(SysUser bean) {

//        bean.setActive(GData.BOOLEAN.YES);
        List<SysUser> list = sysUserDao.selectList(getLQueryWrapper(bean));

        return list;
    }

    /**
     * 新增
     *
     * @author admin
     * @date 2019-02-16 10:22:58
     * @Description:
     */
    @Transactional(rollbackFor = Exception.class)
    public int add(SysUser bean) {

//        bean.setActive(GData.BOOLEAN.YES);
        int res = sysUserDao.insert(bean);

        return res;
    }

    /**
     * 修改
     *
     * @author admin
     * @date 2019-02-16 10:22:58
     * @Description:
     */
    @Transactional(rollbackFor = Exception.class)
    public int update(SysUser bean) {

        int res = sysUserDao.updateById(bean);

        return res;
    }

    /**
     * 删除
     *
     * @author admin
     * @date 2019-02-16 10:22:58
     * @Description:
     */
    @Transactional(rollbackFor = Exception.class)
    public int delete(String ids) {

        List<String> list_id = Stream.of(ids.split(",")).map(String::trim).distinct().filter(StringUtils::isNotEmpty).collect(Collectors.toList());
        //物理删除
        sysUserDao.deleteBatchIds(list_id);

        //逻辑删除
//        list_id.stream().forEach(id -> {
//            SysUser bean = new SysUser();
//            bean.setId(id);
//            bean.setActive(GData.BOOLEAN.NO);
//            bean.setUpdateTime(new Date());
//            sysUserDao.updateById(bean);
//        });

        return list_id.size();
    }

    /**
     * 查询--返回bean
     *
     * @author admin
     * @date 2019-02-16 10:22:58
     * @Description:
     */
    public SysUser search(SysUser bean) {

        return sysUserDao.selectOne(getLQueryWrapper(bean));
    }

    public List<SysUser> aaa() {
        return sysUserDao.aaa();
    }

    /**
     * mybatisplus 分页查询
     *
     * @param gPage
     * @param bean
     * @return
     */
//    public List<SysUser> searchList2(GPage gPage, SysUser bean) {
//        Page<SysUser> page = new Page<>(gPage.getPageNum(), gPage.getPageSize());
//
//        bean.setActive(GData.BOOLEAN.YES);
//        IPage<SysUser> sysUserIPage = sysUserDao.selectPage(page, getLQueryWrapper(bean));
//        gPage = PagePlusUtil.recount(gPage, page);
//        return sysUserIPage.getRecords();
//    }

    /**
     * 自定义查询字段
     * @param bean
     * @return
     */
    public List<SysUser> searchList3(SysUser bean) {

//        LambdaQueryWrapper<SysUser> lqw = new LambdaQueryWrapper();
//        lqw.select(SysUser::getId, SysUser::getAaAge);

        return sysUserDao.selectList(
                getLQueryWrapper(bean).select(SysUser::getId, SysUser::getAaAge)
        );
    }
    /**
     * 自定义查询字段
     * @param bean
     * @return
     */
    public List<Map<String, Object>> searchList4(SysUser bean) {

        return sysUserDao.selectMaps(
                getLQueryWrapper(bean).select(SysUser::getId, SysUser::getAaAge)
        );
    }
}
